module rank {

    export class SmallRank extends egret.DisplayObjectContainer {

        private infos: Array<ScoreItem>;

        private rankItemGroup: egret.DisplayObjectContainer;

        private bgImg: egret.Bitmap;

        private items: Array<SmallRankItem> = [];

        private btnsBox: egret.DisplayObjectContainer;

        private myRankItem: SmallRankItem;

        private pageSize: number = 3;

        public static readonly MAX_WIDTH: number = 550;

        public dispose(): void {
            if (this.items != null && this.items.length > 0) {
                while (this.items.length > 0) {
                    this.items.pop().dispose();
                }
            }
            this.items = null;
        }

        public constructor() {
            super();

            this.bgImg = new egret.Bitmap(CommonRank.spriteSheet.getTexture("rank_bg"));
            CommonRank.WIDTH = this.bgImg.width;
            CommonRank.HEIGHT = this.bgImg.height;

            let item: egret.Bitmap = new egret.Bitmap(CommonRank.spriteSheet.getTexture("rank_item"));
            SmallRankItem.WIDTH = item.width;
            SmallRankItem.HEIGHT = item.height;

            this.rankItemGroup = new egret.DisplayObjectContainer();
            this.addChild(this.rankItemGroup);
            this.rankItemGroup.y = 0;
            this.rankItemGroup.x = 10;

            this.btnsBox = new egret.DisplayObjectContainer();
            this.addChild(this.btnsBox);
        }

        public update(infos: Array<ScoreItem>): void {
            this.infos = infos;
            egret.log("infos.length:", infos.length);
            let tempInfos: Array<ScoreItem> = new Array<ScoreItem>();
            let isFound: boolean = false;
            if (infos.length > 0) {
                for (let i: number = 0; i < infos.length; i++) {
                    tempInfos.push(infos[i]);
                    if (infos[i].openid == Main.openid) {
                        egret.log("找到");
                        if (i + 1 < infos.length) {
                            tempInfos.push(infos[i + 1]);
                        }
                        isFound = true;

                    }
                    while (tempInfos.length > 3) {
                        tempInfos.shift();
                    }
                    if (isFound) {
                        break;
                    }
                }
            }
            this.infos = tempInfos;
            this.layout();
        }

        public static readonly UNIT_WIDTH: number = 180;

        private layout(): void {
            while (this.rankItemGroup.numChildren > 0) {
                helper.DisplayManager.remove(this.rankItemGroup.getChildAt(0));
            }
            for (let i = 0; i < this.pageSize; i++) {
                egret.log("拉取分数信息:", this.infos[i]);
                let item: SmallRankItem;
                if (this.infos[i].openid == Main.openid) {
                    item = new SmallRankItem(this.infos[i], true);
                } else {
                    item = new SmallRankItem(this.infos[i], false);
                }
                this.rankItemGroup.addChild(item);
                // item.x = (i - beginAt) * item.width;
                egret.log("item.width:", item.width);
                item.x = i * SmallRank.UNIT_WIDTH;
                this.items.push(item);
            }
            // this.rankItemGroup.x = SmallRank.MAX_WIDTH - this.rankItemGroup.width >> 1;
        }

    }

}